package server;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.net.ssl.*;
import msgs.*;
/**
 * Listens for incoming admin connections
 * Team 9: Andrew Hayworth, Brian Parrella, Ryan Kortmann, Nina Papa
 * @author Andrew Hayworth
 */

public class AdminListener extends Thread implements TTConstants {
	/**
	 * Queue of incoming admin messages
	 */
	protected static Vector<AdminMsg> inQ;
	
	private boolean go;
	//private SSLServerSocket ssock;
	private ServerSocket ssock;
	private Socket sock;
	private ObjectInputStream ois;
	private AdminMsg amsg;
	//private final static String KS_PATH = "server.jks"; 
	//private final static String KS_PASS = "changeit";
	final String[] enabledCipherSuites = { "SSL_DH_anon_WITH_RC4_128_MD5" };
	
	public AdminListener(int port) { 
		inQ = new Vector<AdminMsg>();
		go = true;
		try {
			//SSLServerSocketFactory factory =
				//(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
			//ssock = (SSLServerSocket) factory.createServerSocket(port);
			
			//ssock.setEnabledCipherSuites(enabledCipherSuites);
			ssock = new ServerSocket(port);
		}
		catch (IOException e) {
			go = false;
			System.err.println("Unable to open port " + port + " for listening!");
			e.printStackTrace();
		}
	}
	public void run() {
		while (go) {
			try {
				// process inQ, block until someone tries to send something
				sock = ssock.accept();
				ois = new ObjectInputStream(sock.getInputStream());
				amsg = (AdminMsg) ois.readObject();
				inQ.add(amsg);
				ois.close();
				sock.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		// TODO shutdown other things
	}
	
	/**
	 * Kills the adminlistener (non-cleanly at this point)
	 */
	public void killAdminListener() {
		go = false;
	}
}
